3 From the UVa Online Judge
5 Author: Andrés Mejía-Posada
6 http://blogaritmo.factorcomun.org
8 Method: Dynamic programming
18 const int maxBoxes
= 1000;
19 const int maxLoad
= 3000;
20 int dp
[maxBoxes
][maxLoad
+1];
22 dp[i][j] = maximum amount of boxes that can be stacked using boxes 0..i (included) such that I can still
23 add j more weight units on top.
28 while (cin
>> n
&& n
){
29 for (int i
=0; i
<n
; ++i
)
30 for (int j
=0; j
<=maxLoad
; ++j
)
34 for (int i
=0; i
<n
; ++i
){
36 cin
>> w
>> l
; //weight and load of box i-th
37 for (int j
=0; j
<= maxLoad
; ++j
){
38 if (j
<= l
) dp
[i
][j
] = 1; //I can stack at least box i-th
39 //But maybe I can create a bigger stack with the same strength not using box i-th
40 if (i
> 0) dp
[i
][j
] = max(dp
[i
][j
], dp
[i
-1][j
]);
42 if (j
+ w
<= maxLoad
&& j
<= l
&& i
> 0){ //Now try to add box i-th on top of the previous boxes
43 dp
[i
][j
] = max(dp
[i
][j
], dp
[i
-1][j
+w
]+1);
47 cout
<< dp
[n
-1][0] << endl
;